home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Resource V24148852001.psc / mGetResource.bas < prev    next >
Encoding:
BASIC Source File  |  2001-07-19  |  16.2 KB  |  426 lines

  1. Attribute VB_Name = "mGetResource"
  2. Private Type ACCEL_TABLE_ENTRY
  3.    fFlags As Integer
  4.    wASCII As Integer
  5.    wID As Integer
  6.    wPadding As Integer
  7. End Type
  8. Private Const FVIRTKEY = &H1
  9. Private Const FNOINVERT = &H2
  10. Private Const FSHIFT = &H4
  11. Private Const FCONTROL = &H8
  12. Private Const FALT = &H10
  13.  
  14. Private Type PictDesc
  15.     cbSizeofStruct As Long
  16.     PicType As Long
  17.     hImage As Long
  18.     xExt As Long
  19.     yExt As Long
  20. End Type
  21. Private Type Guid
  22.     Data1 As Long
  23.     Data2 As Integer
  24.     Data3 As Integer
  25.     Data4(0 To 7) As Byte
  26. End Type
  27.  
  28. Private Declare Function CreateIconFromResourceEx Lib "user32" (presbits As Byte, ByVal dwResSize As Long, ByVal fIcon As Long, ByVal dwVer As Long, ByVal cxDesired As Long, ByVal cyDesired As Long, ByVal uFlags As Long) As Long
  29. Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
  30. Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, riid As Guid, ByVal fPictureOwnsHandle As Long, ipic As IPicture) As Long
  31. Private Const LR_DEFAULTSIZE = &H40
  32. Private Const LR_REALSIZE = &H80
  33. Private Const LR_LOADMAP3DCOLORS = &H1000
  34. Private Const LR_LOADTRANSPARENT = &H20
  35.  
  36. Public Const MAX_STRING = 260
  37.  
  38. Private Type MENUITEMINFO
  39.     cbSize As Long
  40.     fMask As Long
  41.     fType As Long
  42.     fState As Long
  43.     wID As Long
  44.     hSubMenu As Long
  45.     hbmpChecked As Long
  46.     hbmpUnchecked As Long
  47.     dwItemData As Long
  48.     dwTypeData As String
  49.     cch As Long
  50. End Type
  51. Const MIIM_STATE = &H1
  52. Const MIIM_ID = &H2
  53. Const MIIM_SUBMENU = &H4
  54. Const MIIM_CHECKMARKS = &H8
  55. Const MIIM_TYPE = &H10
  56. Const MIIM_DATA = &H20
  57. Const MFT_SEPARATOR = &H800
  58. Const MFS_CHECKED = &H8
  59. Private Declare Function LoadMenu& Lib "user32" Alias "LoadMenuA" (ByVal hInstance As Long, ByVal lpString As String)
  60. Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
  61. Private Declare Function GetMenuItemInfo Lib "user32.dll" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal uItem As Long, ByVal fByPosition As Long, lpmii As MENUITEMINFO) As Long
  62. Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
  63. Dim sMenuText As String
  64.  
  65. Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
  66. Private Declare Function GetFileVersionInfoSize Lib "version.dll" Alias "GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, lpdwHandle As Long) As Long
  67. Private Declare Function GetFileVersionInfo Lib "version.dll" Alias "GetFileVersionInfoA" (ByVal lptstrFilename As String, ByVal dwHandle As Long, ByVal dwLen As Long, lpData As Any) As Long
  68. Private Declare Function VerQueryValue Lib "version.dll" Alias "VerQueryValueA" (pBlock As Any, ByVal lpSubBlock As String, lplpBuffer As Any, puLen As Long) As Long
  69.  
  70. 'Message table structure
  71. Private Type MESSAGE_RESOURCE_BLOCK
  72.    LowId As Long
  73.    HighId As Long
  74.    OffsetToEntries As Long
  75. End Type
  76. Private Type MESSAGE_RESOURCE_ENTRY
  77.   uLength As Integer
  78.   iFlags As Integer
  79.   sText As String
  80. End Type
  81. Private Type MESSAGE_RESOURCE_DATA
  82.    NumberOfBlocks As Long
  83.    mrb() As MESSAGE_RESOURCE_BLOCK
  84.    mre() As MESSAGE_RESOURCE_ENTRY
  85. End Type
  86.  
  87. Private Declare Function FindResource Lib "kernel32" Alias "FindResourceA" (ByVal hInstance As Long, ByVal lpName As String, ByVal lpType As String) As Long
  88. Private Declare Function FindResourceByNum Lib "kernel32" Alias "FindResourceA" (ByVal hInstance As Long, ByVal lpName As String, ByVal lpType As Long) As Long
  89. Private Declare Function LoadResource Lib "kernel32" (ByVal hInstance As Long, ByVal hResInfo As Long) As Long
  90. Private Declare Function LockResource Lib "kernel32" (ByVal hResData As Long) As Long
  91. Private Declare Function SizeofResource Lib "kernel32" (ByVal hInstance As Long, ByVal hResInfo As Long) As Long
  92. Private Declare Function FreeResource Lib "kernel32" (ByVal hResData As Long) As Long
  93.  
  94. Public Function GetPicture(ByVal ResType As String, ByVal ResName As String) As StdPicture
  95.    Dim hData As Long
  96.    Dim arr() As Byte
  97.    Select Case ResType
  98.       Case "1", "3" 'Hardware dependent cursor or icon.
  99.          arr = GetDataArray(ResType, ResName)
  100.          hData = CreateIconFromResourceEx(arr(0), UBound(arr) + 1, CLng(ResType) - 1, &H30000, 0, 0, LR_LOADMAP3DCOLORS)
  101.       Case "2" 'Bitmap
  102.          hData = LoadImage(hModule, ResName, 0, 0, 0, LR_LOADMAP3DCOLORS)
  103.       Case "12" 'Hardware independent cursor
  104.          hData = LoadImage(hModule, ResName, 2, 0, 0, LR_LOADMAP3DCOLORS)
  105.       Case "14" 'Hardware independent icon
  106.          hData = LoadImage(hModule, ResName, 1, 0, 0, LR_LOADMAP3DCOLORS)
  107.    End Select
  108.    If hData = 0 Then Exit Function
  109.    If ResType = "2" Then
  110.       Set GetPicture = BitmapToPicture(hData)
  111.    Else
  112.       Set GetPicture = IconToPicture(hData)
  113.    End If
  114. End Function
  115.  
  116. Public Function GetPictureExt(ByVal ResType As String, ByVal ResName As String) As IPictureDisp
  117.    Dim arr() As Byte
  118.    Dim nFile As Integer
  119.    Dim pic As IPictureDisp
  120.    Dim nWidth As Long, nHeight As Long
  121.    Dim sType As String
  122.    If Dir(TEMP_FILE_NAME) <> "" Then
  123.       Call mciSendString("close video", 0&, 0, 0)
  124.       Kill TEMP_FILE_NAME
  125.    End If
  126.    arr = GetDataArray(ResType, ResName)
  127.    nFile = FreeFile
  128.    Open TEMP_FILE_NAME For Binary As #nFile
  129.       Put #nFile, , arr
  130.    Close #nFile
  131.    Set GetPictureExt = LoadPicture(TEMP_FILE_NAME)
  132.    Kill TEMP_FILE_NAME
  133. End Function
  134.  
  135. Private Function BitmapToPicture(ByVal hBmp As Long) As StdPicture
  136.     Dim oNewPic As Picture, tPicConv As PictDesc, IGuid As Guid
  137.     With tPicConv
  138.        .cbSizeofStruct = Len(tPicConv)
  139.        .PicType = vbPicTypeBitmap
  140.        .hImage = hBmp
  141.     End With
  142.     With IGuid
  143.        .Data1 = &H20400
  144.        .Data4(0) = &HC0
  145.        .Data4(7) = &H46
  146.     End With
  147.     OleCreatePictureIndirect tPicConv, IGuid, True, oNewPic
  148.     Set BitmapToPicture = oNewPic
  149. End Function
  150.  
  151. Private Function IconToPicture(ByVal hIcon As Long) As StdPicture
  152.     If hIcon = 0 Then Exit Function
  153.     Dim oNewPic As Picture
  154.     Dim tPicConv As PictDesc
  155.     Dim IGuid As Guid
  156.     With tPicConv
  157.        .cbSizeofStruct = Len(tPicConv)
  158.        .PicType = vbPicTypeIcon
  159.        .hImage = hIcon
  160.     End With
  161.     With IGuid
  162.         .Data1 = &H7BF80980
  163.         .Data2 = &HBF32
  164.         .Data3 = &H101A
  165.         .Data4(0) = &H8B
  166.         .Data4(1) = &HBB
  167.         .Data4(2) = &H0
  168.         .Data4(3) = &HAA
  169.         .Data4(4) = &H0
  170.         .Data4(5) = &H30
  171.         .Data4(6) = &HC
  172.         .Data4(7) = &HAB
  173.     End With
  174.     OleCreatePictureIndirect tPicConv, IGuid, True, oNewPic
  175.     Set IconToPicture = oNewPic
  176. End Function
  177.  
  178. Public Function GetString(ByVal ResName As String) As String
  179.    Dim arr() As Byte
  180.    Dim nPos As Long, wID As Long, uLength As Long
  181.    Dim s As String, sText As String
  182.    arr = GetDataArray("6", ResName)
  183.    For wID = (CLng(Mid(ResName, 2)) - 1) * 16 To CLng(Mid(ResName, 2)) * 16 - 1
  184.        Call CopyMemory(uLength, arr(nPos), 2)
  185.        If uLength Then
  186.           s = String(uLength, 0)
  187.           CopyMemory ByVal StrPtr(s), arr(nPos + 2), uLength * 2
  188.           s = CStr(wID) & ": " & s
  189. 'Strings in resources sometimes delimited with vbLf(chr(10))
  190. 'and sometimes with vbCrLf (Chr(10) & Chr(13))
  191. 'There is no differense for messageboxes, but textbox show
  192. 'unreadable symbols for vbLf, so here is work around
  193. 'VB6 users can use internal Replace function
  194.           s = ReplaceStr(s, vbLf, vbNewLine)
  195.           s = ReplaceStr(s, vbCr & vbNewLine, vbNewLine)
  196.           sText = sText & TrimNULL(s) & vbNewLine
  197.           nPos = nPos + uLength * 2 + 2
  198.        Else
  199.           nPos = nPos + 2
  200.        End If
  201.    Next wID
  202.    GetString = sText
  203. End Function
  204.  
  205. Public Function GetMenuText(ByVal ResName As String) As String
  206.    Dim hMenu As Long
  207.    sMenuText = ""
  208.    hMenu = LoadMenu(hModule, ResName)
  209.    GetMenuInfo hMenu, 0
  210.    DestroyMenu hMenu
  211.    GetMenuText = sMenuText
  212. End Function
  213.  
  214. Private Sub GetMenuInfo(ByVal hMenu As Long, ByVal level As Long)
  215.     Dim itemcount As Long
  216.     Dim c As Long
  217.     Dim mii As MENUITEMINFO
  218.     Dim retval As Long
  219.     itemcount = GetMenuItemCount(hMenu)
  220.     With mii
  221.         .cbSize = Len(mii)
  222.         .fMask = MIIM_STATE Or MIIM_TYPE Or MIIM_SUBMENU Or MIIM_ID
  223.         For c = 0 To itemcount - 1
  224.             .dwTypeData = Space(256)
  225.             .cch = 256
  226.             retval = GetMenuItemInfo(hMenu, c, 1, mii)
  227.             If mii.fType = MFT_SEPARATOR Then
  228.                sMenuText = sMenuText & String(5 * level, ".") & "[MENU SEPARATOR]" & vbNewLine
  229.             Else
  230.                sMenuText = sMenuText & String(5 * level, ".") & Left(.dwTypeData, .cch)
  231.                If (.fState And MFS_CHECKED) Then sMenuText = sMenuText & " (checked)"
  232.                sMenuText = sMenuText & " (cmdID = " & .wID & ")" & vbNewLine
  233.             End If
  234.             If .hSubMenu <> 0 Then GetMenuInfo .hSubMenu, level + 1
  235.         Next c
  236.     End With
  237. End Sub
  238.  
  239. Public Function GetAccelerators(ByVal ResName As String) As String
  240.    Dim nItems As Long, i As Long
  241.    Dim arr() As Byte
  242.    Dim ate() As ACCEL_TABLE_ENTRY
  243.    Dim sText As String
  244.    arr = GetDataArray("9", ResName)
  245.    nItems = (UBound(arr) + 1) \ 8
  246.    ReDim ate(nItems - 1)
  247.    Call CopyMemory(ate(0), arr(0), nItems * 8)
  248.    For i = 0 To nItems - 1
  249.        If (ate(i).fFlags And FSHIFT) Then sText = sText & "Shift+"
  250.        If (ate(i).fFlags And FCONTROL) Then sText = sText & "Ctrl+"
  251.        If (ate(i).fFlags And FALT) Then sText = sText & "Alt+"
  252.        sText = sText & KeyName(ate(i).wASCII)
  253.        sText = sText & " (cmdID = " & ate(i).wID & ")" & vbNewLine
  254.    Next i
  255.    GetAccelerators = sText
  256. End Function
  257.  
  258. Private Function KeyName(ByVal key As Long) As String
  259.   Select Case key
  260.      Case vbKeyA To vbKeyZ, vbKey0 To vbKey9
  261.           KeyName = Chr(key)
  262.      Case vbKeyF1 To vbKeyF16
  263.           KeyName = "F" & CStr(key - vbKeyF1 + 1)
  264.      Case vbKeyCancel:   KeyName = "CANCEL"
  265.      Case vbKeyBack:     KeyName = "BACKSPACE"
  266.      Case vbKeyTab:      KeyName = "TAB"
  267.      Case vbKeyClear:    KeyName = "CLEAR"
  268.      Case vbKeyReturn:   KeyName = "ENTER"
  269.      Case vbKeyShift:    KeyName = "SHIFT"
  270.      Case vbKeyControl:  KeyName = "CTRL"
  271.      Case vbKeyMenu:     KeyName = "MENU"
  272.      Case vbKeyPause:    KeyName = "PAUSE"
  273.      Case vbKeyCapital:  KeyName = "CAPS LOCK"
  274.      Case vbKeyEscape:   KeyName = "ESC"
  275.      Case vbKeySpace:    KeyName = "SPACEBAR"
  276.      Case vbKeyPageUp:   KeyName = "PAGE UP"
  277.      Case vbKeyPageDown: KeyName = "PAGE DOWN"
  278.      Case vbKeyEnd:      KeyName = "END"
  279.      Case vbKeyHome:     KeyName = "HOME"
  280.      Case vbKeyLeft:     KeyName = "LEFT ARROW"
  281.      Case vbKeyUp:       KeyName = "UP ARROW"
  282.      Case vbKeyRight:    KeyName = "RIGHT ARROW"
  283.      Case vbKeyDown:     KeyName = "DOWN ARROW"
  284.      Case vbKeySelect:   KeyName = "SELECT"
  285.      Case vbKeyPrint:    KeyName = "PRINT SCREEN"
  286.      Case vbKeyExecute:  KeyName = "EXECUTE"
  287.      Case vbKeySnapshot: KeyName = "SNAPSHOT"
  288.      Case vbKeyInsert:   KeyName = "INS"
  289.      Case vbKeyDelete:   KeyName = "DEL"
  290.      Case vbKeyHelp:     KeyName = "HELP"
  291.      Case vbKeyNumlock:  KeyName = "NUM LOCK"
  292.      Case Else:          KeyName = "Virtual Key " & CStr(key)
  293.   End Select
  294. End Function
  295.  
  296. Public Function GetHexDump(ByVal ResType As String, ByVal ResName As String) As String
  297.    Dim arr() As Byte
  298.    Dim sText As String, sLine As String
  299.    arr = GetDataArray(ResType, ResName)
  300.    sText = Space$((UBound(arr) \ 16 + 1) * 79)
  301.    If Len(sText) > 65534 Then
  302.       GetHexDump = sText
  303.       Exit Function
  304.    End If
  305.    On Error Resume Next
  306.    For i = 0 To UBound(arr) - 1 Step 16
  307.        sLine = ZeroPad(Hex(i), 8) & " | "
  308.        For j = 0 To 15
  309.            sLine = sLine & ZeroPad(Hex(arr(i + j)), 2) & " "
  310.            If Err Then sLine = sLine & "   "
  311.        Next j
  312.        sLine = sLine & "| "
  313.        For j = 0 To 15
  314.            If arr(i + j) < 32 Then
  315.               sLine = sLine & "."
  316.            Else
  317.               sLine = sLine & Chr(arr(i + j))
  318.            End If
  319.        Next j
  320.        sLine = sLine & vbNewLine
  321.        Mid(sText, (i \ 16) * 79 + 1, 79) = sLine
  322.    Next i
  323.    GetHexDump = sText
  324. End Function
  325.  
  326. Public Function GetDataArray(ByVal ResType As String, ByVal ResName As String) As Variant
  327.    Dim hRsrc As Long
  328.    Dim hGlobal As Long
  329.    Dim arrData() As Byte
  330.    Dim lpData As Long
  331.    Dim arrSize As Long
  332.    If IsNumeric(ResType) Then hRsrc = FindResourceByNum(hModule, ResName, CLng(ResType))
  333.    If hRsrc = 0 Then hRsrc = FindResource(hModule, ResName, ResType)
  334.    If hRsrc = 0 Then Exit Function
  335.    hGlobal = LoadResource(hModule, hRsrc)
  336.    lpData = LockResource(hGlobal)
  337.    arrSize = SizeofResource(hModule, hRsrc)
  338.    If arrSize = 0 Then Exit Function
  339.    ReDim arrData(arrSize - 1)
  340.    Call CopyMemory(arrData(0), ByVal lpData, arrSize)
  341.    Call FreeResource(hGlobal)
  342.    GetDataArray = arrData
  343. End Function
  344.  
  345. Public Function GetVersionInfo(ByVal ResName As String) As String
  346.    Dim arrVerInfo() As Byte
  347.    Dim arrInfoName As Variant
  348.    Dim arrLang(3) As Byte
  349.    Dim sLang As String
  350.    Dim dwBytes As Long
  351.    Dim lpBuffer As Long
  352.    Dim s As String
  353.    Dim sText As String
  354.    s = String(MAX_STRING, 0)
  355.    Call GetModuleFileName(hModule, s, MAX_STRING)
  356.    s = TrimNULL(s)
  357.    dwBytes = GetFileVersionInfoSize(s, lpBuffer)
  358.    ReDim arrVerInfo(0 To dwBytes - 1)
  359.    Call GetFileVersionInfo(s, 0, dwBytes, arrVerInfo(0))
  360.    arrInfoName = Array("OriginalFilename", "InternalName", "FileVersion", "FileDescription", "ProductName", "ProductVersion", "CompanyName", "LegalCopyright")
  361.    Call VerQueryValue(arrVerInfo(0), "\VarFileInfo\Translation", lpBuffer, dwBytes)
  362.    Call CopyMemory(arrLang(0), ByVal lpBuffer, dwBytes)
  363.    sLang = ZeroPad(Hex(arrLang(1)), 2) & ZeroPad(Hex(arrLang(0)), 2) & ZeroPad(Hex(arrLang(3)), 2) & ZeroPad(Hex(arrLang(2)), 2)
  364.    For i = 0 To UBound(arrInfoName) - 1
  365.        Call VerQueryValue(arrVerInfo(0), "\StringFileInfo\" & sLang & "\" & CStr(arrInfoName(i)), lpBuffer, dwBytes)
  366.        s = StrFromPtrA(lpBuffer)
  367.        If s <> "" Then sText = sText & arrInfoName(i) & ":" & vbCrLf & vbTab & s & vbNewLine
  368.    Next i
  369.    GetVersionInfo = Trim(sText)
  370. End Function
  371.  
  372. Private Function ZeroPad(strValue As String, intLen As String) As String
  373.     ZeroPad = Right$(String(intLen, "0") & strValue, intLen)
  374. End Function
  375.  
  376. Public Function GetMessageTable(ByVal ResName As String) As String
  377.    Dim arr() As Byte
  378.    Dim nBlocks As Long
  379.    Dim nPos As Long
  380.    Dim i As Long, j As Long
  381.    Dim s As String, sText As String
  382.    Dim uLength As Long, uFlag As Long
  383.    arr = GetDataArray("11", ResName)
  384.    Call CopyMemory(nBlocks, arr(0), 4)
  385.    If nBlocks = 0 Then Exit Function
  386.    ReDim mrb(nBlocks - 1) As MESSAGE_RESOURCE_BLOCK
  387.    Call CopyMemory(mrb(0), arr(4), 12 * nBlocks)
  388.    For i = 0 To nBlocks - 1
  389.        nPos = mrb(i).OffsetToEntries
  390.        For j = mrb(i).LowId To mrb(i).HighId
  391.            Call CopyMemory(uLength, arr(nPos), 2)
  392.            If uLength Then
  393.               Call CopyMemory(uFlag, arr(nPos + 2), 2)
  394.               If uFlag = 1 Then 'Unicode
  395.                  s = String(uLength, Chr$(0))
  396.                  CopyMemory ByVal StrPtr(s), arr(nPos + 4), uLength - 4
  397.                  s = CStr(j) & ": " & s
  398.               Else 'ANSI
  399.                  s = CStr(j) & ": " & StrFromPtrA(VarPtr(arr(nPos + 4)), uLength - 4)
  400.               End If
  401. 'See comments to GetString function
  402.               s = ReplaceStr(s, vbLf, vbNewLine)
  403.               s = ReplaceStr(s, vbCr & vbNewLine, vbNewLine)
  404.               sText = sText & TrimNULL(s)
  405.               nPos = nPos + uLength
  406.            End If
  407.        Next j
  408.    Next i
  409.    GetMessageTable = sText
  410. End Function
  411.  
  412. Public Function GetHTML(ByVal ResType As String, ByVal ResName As String) As String
  413.   GetHTML = StrConv(GetDataArray(ResType, ResName), vbUnicode)
  414. End Function
  415.  
  416. Public Function ResSize(ByVal ResType As String, ByVal ResName As String) As Long
  417.    Dim hRsrc As Long, hGlobal As Long
  418.    If IsNumeric(ResType) Then hRsrc = FindResourceByNum(hModule, ResName, CLng(ResType))
  419.    If hRsrc = 0 Then hRsrc = FindResource(hModule, ResName, ResType)
  420.    If hRsrc = 0 Then Exit Function
  421.    hGlobal = LoadResource(hModule, hRsrc)
  422.    ResSize = SizeofResource(hModule, hRsrc)
  423.    Call FreeResource(hGlobal)
  424. End Function
  425.  
  426.